package snooker.scoreboard.event;

import snooker.scoreboard.activity.FrameHistoryActivity;
import snooker.scoreboard.activity.FrameStatisticsActivity;
import snooker.scoreboard.activity.MatchStatisticsActivity;
import snooker.scoreboard.activity.R;
import android.app.Activity;
import android.content.Intent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class StartActivityTouchListener implements OnTouchListener {

	private static final int DIFF = 100;
	private Activity act;
	private float downX = -1;
	private float downY = -1;
	
	public StartActivityTouchListener(Activity act) {
		this.act = act;
	}
	
	public boolean onTouch(View v, MotionEvent event) {
		int action = event.getAction();
		switch (action) { 
		case (MotionEvent.ACTION_DOWN) : // Touch screen pressed
			downX = event.getX();
			break; 
		case (MotionEvent.ACTION_UP) : // Touch screen touch ended
			downX = -1;
			downY = -1;
			break; 
		case (MotionEvent.ACTION_MOVE) : // Contact has moved across screen
			float x = event.getX();
			float y = event.getY();
			if (downX == -1 || downY == -1) {
				downX = x;
				downY = y;
			}
			//Log.i("", x + " - " + downX);
			if ((downX - x) > DIFF) {
				downX = -1;
				Intent i = new Intent(act,FrameHistoryActivity.class);
				i.putExtra("changedirection", true);
				i.putExtra("frameid", -1);
				act.startActivity(i);
				act.overridePendingTransition(
						R.anim.slide_in_from_right, 
						R.anim.slide_out_to_left);
			} else if ((x - downX) > DIFF) {
				downX = -1;
				Intent i = new Intent(act,MatchStatisticsActivity.class);
				i.putExtra("changedirection", true);
				act.startActivity(i);
				act.overridePendingTransition(
						R.anim.slide_in_from_left, 
						R.anim.slide_out_to_right);
			} else if ((y - downY) > 60) {
					downY = -1;
					Intent i = new Intent(act,FrameStatisticsActivity.class);
					i.putExtra("frameid", -1);
					i.putExtra("changedirection", true);
					act.startActivity(i);
					act.overridePendingTransition(
							R.anim.slide_in_from_top, 
							R.anim.slide_out_to_bottom);
			}
			break; 
		case (MotionEvent.ACTION_CANCEL) : // Touch event cancelled
			break;
		} 
		return true;
	}
}
